//
//  MainViewController.swift
//  testDemo
//
//  Created by frank on 2021/4/14.
//  Copyright © 2021 Alexey Naumov. All rights reserved.
//

import Foundation

class MainViewController: BaseTableViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.isHasHeaderView(true)
        self.isHasFooterView(true)
        self.tableView.k_height = kCNScreenH - kTabAndBottomHeight - KNavAndStatusHeight
        self.initData()
    }
    
    func initData() -> Void {
        self.dataSource.removeAll()
        let titleArray = ["vc1","vc2","vc3"];
        let pushVCArray = ["BaseViewController","BaseViewController","BaseViewController"];
        self.dataSource = [CommonModel]()
        for i in 0..<titleArray.count {
            var model = CommonModel.init()
            model.cellTitle = titleArray[i]
            model.cellContent = pushVCArray[i]
            self.dataSource.append(model)
        }
        self.tableView.reloadData()
    }
    
    override func updateData() {
        super.updateData()
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.5) {
            self.tableView.mj_header.endRefreshing()
            self.tableView.mj_footer.endRefreshing()
        }
    }
    
    override func loadMoreData() {
        super.loadMoreData()
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.5) {
            self.tableView.mj_header.endRefreshing()
            self.tableView.mj_footer.endRefreshing()
        }
    }
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        if (cell == nil) {
            cell = UITableViewCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: "cell")
        }
        cell?.textLabel?.text = self.dataSource[indexPath.row].cellTitle
        return cell!
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let model = self.dataSource[indexPath.row]
        if (model.cellContent.count != 0) {
            let nameSpace = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
            let classType:AnyClass? = NSClassFromString(nameSpace + "." + model.cellContent)
            let viewController = classType as! UIViewController.Type
            let vc = viewController.init()
            vc.title = model.cellTitle
//            self.navigationController?.pushViewController(vc , animated: true)
            self.swe.pushVC(vc)
        }
    }
}


